﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Trippy.CollidableGameComponents;
using Trippy.CollidableGameComponents.Agents;
using Trippy.Levels;
using Trippy.Animation;

namespace Trippy.Abilities
{
    class Life : Ability
    {
        EffectAnimation Channel;

        public Life(Level level)
            : base(level)
        {
            name = "Life";
            icon = CurrentLevel.Game.Content.Load<Texture2D>("Sprites/Ability Icons/life");
        }

        public override void OnLand()
        {
            if(State != AbilityState.Active)
                State = AbilityState.Available;
            base.OnLand();
        }

        public override void OnAir()
        {
            State = AbilityState.Unavailable;
            OnCancel();
            base.OnAir();
        }
        
        public override void OnActivate()
        {
            State = AbilityState.Active;
            CurrentLevel.Hero.Velocity = new Vector2();
            SetTimer(5f);

            Channel = new EffectAnimation(CurrentLevel,
                CurrentLevel.Game.Content.Load<Texture2D>("Sprites/Effects/castheal"),
                64, 64, 0, 0, 0, 0, 15, 0.15f, 20, CurrentLevel.Hero.Position + new Vector2(0, 10));

            base.OnActivate();
        }

        public override void OnMoveLeft(float elapsedTime)
        {
            OnCancel();
            base.OnMoveLeft(elapsedTime);
        }

        public override void OnMoveRight(float elapsedTime)
        {
            OnCancel();
            base.OnMoveRight(elapsedTime);
        }

        public override void OnJump()
        {
            OnCancel();
            base.OnJump();
        }

        public override void OnRemove()
        {
            OnCancel();
            base.OnRemove();
        }

        public override void OnCancel()
        {
            if (State == AbilityState.Active)
            {
                State = AbilityState.Available;
                KillTimer();
                Channel.StopAnimation();
            }
            base.OnCancel();
        }

        protected override void OnTimer()
        {
            CurrentLevel.Hero.RestoreAbility();
            Hero.Lives++;
            Channel.StopAnimation();

            base.OnTimer();
        }

        public override void OnDraw(SpriteBatch sb, Rectangle r)
        {
            base.OnDraw(sb, r);
            sb.DrawString(font, "1", new Vector2(r.X + 7, r.Y + r.Height - 20), CurrentLevel.UIColor);
        }
    }
}
